package cn.xiaoxiong.shop.Controller;

import cn.hutool.json.JSONUtil;
import cn.xiaoxiong.shop.models.GoodsForm;
import cn.xiaoxiong.shop.util.SignUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@Controller
public class ShopController {
    private String userNo="81764817292347027458";
    private String appKey="d9f2ff973b389ec5028f748b35c313fc";
    private String payProductCode="node001";
    private String pageUrl="http://localhost:8081/show.html";
    private String notifyUrl="http://localhost:8081/notify";
    private String appSecret="11647ac669bf55c854ab66b9b7216079";
    @RequestMapping("/buy")
    public String buy(GoodsForm goodsForm, Model model){
        String orderTime = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        Map<String,String> map = SignUtil.jsonToMap(JSONUtil.toJsonStr(goodsForm));
        map.put("userNo",userNo);
        map.put("appKey",appKey);
        map.put("payProductCode",payProductCode);
        map.put("pageUrl",pageUrl);
        map.put("notifyUrl",notifyUrl);
        map.put("orderTime",orderTime);
        String sign = SignUtil.createSign(map,appSecret);

        map.put("sign",sign);
        model.addAllAttributes(map);
        return "/pay";
    }
    @RequestMapping("/notify")
    public String handleNotify(){
        return "ok";
    }

}
